package com.wang.book.service.impl;

import com.wang.book.exception.BusinessException;
import com.wang.book.mapper.BookMapper;
import com.wang.book.mapper.BookTypeMapper;
import com.wang.book.service.BookTypeService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
public class BookTypeServiceImpl implements BookTypeService {
    @Autowired
    private BookTypeMapper bookTypeMapper;
    @Autowired
    private BookMapper bookMapper;

    @Transactional
    public void deleteType(Integer typeNo) {
        if (bookTypeMapper.countBooksByType(typeNo) > 0) {
            throw new BusinessException("存在关联图书，无法删除分类");
        }
        bookTypeMapper.deleteById(typeNo);
    }
}
